
//% color="#A0522D" iconWidth=50 iconHeight=40
namespace pytesseract{
    //% block="初始化摄像头直到成功 编号[CAMNUM]" blockType="command"
    //% CAMNUM.shadow="number" CAMNUM.defl="0"
    export function matplotlib_Iint(parameter: any, block: any) {
        let num=parameter.CAMNUM.code;

        Generator.addImport(`import time\nimport cv2\nimport pytesseract\nfrom PIL import Image,ImageFont,ImageDraw\nimport os\nimport numpy as np`)
        Generator.addCode(`def drawChinese(text,x,y,size,r, g, b, a,img):
    font = ImageFont.truetype("HYQiHei_50S.ttf", size)
    img_pil = Image.fromarray(img)
    draw = ImageDraw.Draw(img_pil)
    draw.text((x,y), text, font=font, fill=(b, g, r, a))
    frame = np.array(img_pil)
    return frame\n`) 
        Generator.addCode(`pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'`)
        Generator.addCode(`cap = cv2.VideoCapture(${num})`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_FRAME_WIDTH, 240)`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)`)
        Generator.addCode(`cv2.namedWindow('cvwindow',cv2.WND_PROP_FULLSCREEN)`)
        Generator.addCode(`cv2.setWindowProperty('cvwindow', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)`)            
    Generator.addCode(`while not cap.isOpened():
    continue`)    
    }
    
    //% block="打开摄像头画面进行读取" blockType="command"
    export function matplotlib_Iintsize(parameter: any, block: any) {
        
        Generator.addCode(`cv2.waitKey(5)
cvimg_success, img_src = cap.read()
cvimg_h, cvimg_w, cvimg_c = img_src.shape
cvimg_w1 = cvimg_h*240//320
cvimg_x1 = (cvimg_w-cvimg_w1)//2
img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
img_src = cv2.resize(img_src, (240, 320))
cv2.imshow('cvwindow', img_src)`)
    } 

    //% block="保存图片到文件夹 路径[PATH]返回识别内容[LIST] " blockType="command"
    //% PATH.shadow="string" PATH.defl="/root/pytesseract/pic/"
    //% LIST.shadow="normal" LIST.defl="text"
    export function matplotlib_rcParams(parameter: any, block: any) {
        let path=parameter.PATH.code;
        let list=parameter.LIST.code;
            
        path=replaceQuotationMarks(path)
        
        
        Generator.addCode(`${list} = []
try:
    if not os.path.exists("${path}"):
        print("The folder does not exist,created automatically")
        os.system("mkdir -p ${path}")
except IOError:
    print("IOError,created automatically")
    break        
cv2.imwrite("/root/pytesseract/pic/image.png",img_src)
time.sleep(0.2)
img = Image.open('/root/pytesseract/pic/image.png')
${list} = pytesseract.image_to_string(img, lang='chi_sim')   
`)
    }


    //% block="在摄像头画面上显示文字[TEXT]大小[SIZE] 颜色R[R]G[G]B[B] 坐标X[X]Y[Y]" blockType="command"
    //% TEXT.shadow="string" TEXT.defl="你好"
    //% SIZE.shadow="number"   SIZE.defl="25"
    //% R.shadow="range"   R.params.min=0    R.params.max=255    R.defl=50
    //% G.shadow="range"   G.params.min=0    G.params.max=255    G.defl=200
    //% B.shadow="range"   B.params.min=0    B.params.max=255    B.defl=0
    //% X.shadow="number"   X.defl="10"
    //% Y.shadow="number"   Y.defl="20"
    export function drawText(parameter: any, block: any) {
        let txt=parameter.TEXT.code;
        let size=parameter.SIZE.code;
        let r=parameter.R.code;
        let g=parameter.G.code;
        let b=parameter.B.code;
        let x=parameter.X.code;
        let y=parameter.Y.code;

        //Generator.addCode(`cv2.putText(img_src, str(${txt}), (${x}, ${y}), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (${b}, ${g}, ${r}), 2)`)   
         
        Generator.addCode(`img_src = drawChinese(text=str(${txt}),x=${x}, y=${y},size=${size},r= ${r},g=${g},b=0,a=${b},img=img_src)\ncv2.imshow('cvwindow', img_src)`)    
    }

    function replaceQuotationMarks(str:string){
        str=str.replace(/"/g, ""); //去除所有引号
        return str
}

}
